package com.example.t410.service.serviceImpl;

import com.example.t410.dao.PsychologicalTest;
import com.example.t410.model.TestReturn;
import com.example.t410.service.PsychologicalTestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class PsychologicalTestServiceImpl implements PsychologicalTestService {

    private static ArrayList<String> good = new ArrayList<>();
    private static ArrayList<String> bad = new ArrayList<>();
    @Autowired
    MongoTemplate mongoTemplate;
    @Override
    public List<PsychologicalTest> findAll() {
        return mongoTemplate.findAll(PsychologicalTest.class);
    }

    @Override
    public TestReturn calculateAns(String ans) {
        char[] all =ans.toCharArray();
        int[] shift = new int[60];
        int[] relation = {11,13,18,20,35,7,43,50,55,56};
        int[] study = {4,9,24,28,30,34,40,44,47,49,59};
        int[] campus = {17,22,26,32,38,46,54,60};
        int[] job = {5,8,12,14,19,36,41,51,57};
        int[] emo = {1,3,15,21,27,39,45,53,58};
        int[] self = {6,10,16,23,25,29,33,42};
        int[] sat = {2,7,31,48,52};
        int[] change = {5,6,12,18,19,20,23,26,28,30,32,38,40,42,45,48,53,55,57};
        double sum=0.0;
        for(int i = 0,j = 0;i<all.length;i++)
        {
            shift[i]=all[i]-'0';
            if(j<change.length&&i==(change[j]-1)) {
                j++;
                shift[i] = 6 - shift[i];
            }
            sum+=shift[i];
        }
        sum*=(1.0/3);
        int f = (int) Math.round(sum);
        TestReturn testReturn = new TestReturn();
        if(f>=51)testReturn.setJudge("适应能力总体较好");
        else testReturn.setJudge("适应能力总体有待提高");
        testReturn.setRelationship(calculate(relation,shift,"人际关系适应能力"));
        testReturn.setStudy(calculate(study,shift,"学习适应能力"));
        testReturn.setCampus(calculate(campus,shift,"校园适应能力"));
        testReturn.setJob(calculate(job,shift,"择业适应能力"));
        testReturn.setEmotion(calculate(emo,shift,"情绪适应能力"));
        testReturn.setSelf(calculate(self,shift,"自我适应能力"));
        testReturn.setSatisfaction(calculate(sat,shift,"满意度"));
        testReturn.setTotal(String.valueOf(f));
        String[] s1 =good.toArray(new String[good.size()]);
        testReturn.setGood(s1);
        good.clear();
        String[] s2 = bad.toArray(new String[bad.size()]);
        testReturn.setBad(s2);
        bad.clear();
        return testReturn;
    }
    private static String calculate(int[] indexs,int[] res,String signature)
    {
        double sum=0;
        for (int j : indexs) sum += res[j - 1];
        double ta = 5*indexs.length;
        sum *= 100/ta;
        int f = (int) Math.round(sum);
        if(f>=51)good.add(signature);
        else bad.add(signature);
        return Integer.toString(f);
    }

}
